package com.skycong.hello77.activity.register;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.skycong.hello77.R;
import com.skycong.hello77.service.UserService;
import com.skycong.hello77.util.Result;
import com.skycong.hello77.util.Validator;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * Created by wangqi on 18/1/11.
 */

public class RegisterActivity extends Activity implements View.OnFocusChangeListener, View.OnClickListener {
    private UserService userService;
    private EditText registerName;//注册名
    private EditText registerPassword;//注册密码
    private EditText rePassword;//确认密码
    private TextView informationName;
    private TextView informationPassword;
    private TextView informationRePWD;
    private TextView register;//注册
    private Context context = RegisterActivity.this;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //强制在住Activity中执行网络操作
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        //创建userServe对象
        userService = new UserService(this);

        registerName = (EditText) findViewById(R.id.register_username);
        registerName.setOnFocusChangeListener(this);
        registerPassword = (EditText) findViewById(R.id.register_password);
        registerPassword.setOnFocusChangeListener(this);
        rePassword = (EditText) findViewById(R.id.re_password);
        rePassword.setOnFocusChangeListener(this);
        informationName = (TextView) findViewById(R.id.information_name);
        informationPassword = (TextView) findViewById(R.id.information_password);
        informationRePWD = (TextView) findViewById(R.id.information_repassword);
        register = (TextView) findViewById(R.id.register);
        register.setOnClickListener(this);

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int id = v.getId();
        System.out.println("失去焦点执行了");
        String email = registerName.getText().toString().trim();
        String password = registerPassword.getText().toString().trim();
        String rePWD = rePassword.getText().toString().trim();

        /*
         * 判断邮箱的
         * */

        if (!hasFocus && id == R.id.register_username) {
            if (Validator.isEmail(email)) {
                // TODO: 18/1/12 校验邮箱是否已经被注册
                Result result = userService.checkEmailRequest(registerName.getText().toString().trim());
                if (result.getStatus() == 200) {
                    informationName.setTextColor(Color.GREEN);
                    informationName.setText("该邮箱可以使用");
                    setLoginEnable();
                } else if (result.getStatus() == 400) {
                    informationName.setTextColor(Color.RED);
                    informationName.setText("该邮箱已存在");
                    setLoginDisable();
                }
            } else {
                informationName.setText("邮箱格式不正确");
                setLoginDisable();
            }
        }
        /*
         * 判断密码的
         * */
        if (!hasFocus && id == R.id.register_password) {
            if (!Validator.isPassword(password)) {
                informationPassword.setText("密码至少为六位数");
                setLoginDisable();
            } else {
                informationPassword.setText(" ");
                setLoginEnable();
            }
        }
        /*
         * 判断确认密码的
         * */
        if (!hasFocus && id == R.id.re_password) {
            if (!password.equals(rePWD)) {
                informationRePWD.setText("密码不一致");
                setLoginDisable();
            } else {
                informationRePWD.setText("");
                setLoginEnable();
            }
        }

    }

    @Override
    public void onClick(View v) {

        Log.w("aaa", "in onclick reg");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("email", registerName.getText().toString());
        map.add("password", registerPassword.getText().toString());
        Log.w("aaa", "send reg request");
        Result result = userService.registerRequest(map);
        Log.w("aaa", "android server response = " + result);
        if (result.getStatus() == 200) {
            Log.i("log", "reg success");
            Toast.makeText(this, "注册成功", Toast.LENGTH_LONG).show();
        } else if (result.getStatus() == 400) {
            Log.i("log", "reg failed");
            Toast.makeText(this, "注册失败", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 验证不成功 register设为不可点击
     */
    private void setLoginDisable() {
        register.setBackgroundColor(Color.GRAY);
        register.setEnabled(false);
    }

    private void setLoginEnable() {
        register.setBackgroundColor(Color.BLACK);
        register.setEnabled(true);
    }

}
